package cn.platform.controller;



import cn.platform.entity.StuHomework;
import cn.platform.request.StuHomework.StuHomeworkQueryDTO;
import cn.platform.request.StuHomework.StuHomeworkUpdateDTO;
import cn.platform.response.PageResult;
import cn.platform.response.Result;
import cn.platform.response.StuHomeworkVO;
import cn.platform.service.StuHomeworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Year;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/homework")
public class StuHomeworkController {

    @Autowired
    private StuHomeworkService stuHomeworkService;

    // 新增：获取学生列表接口
    @GetMapping("/student")
    public Result getStudents(@RequestParam(required = false) String name) {
        return Result.success(stuHomeworkService.getStudents(name));
    }

    // 新增：获取老师列表接口
    @GetMapping("/teacher")
    public Result getTeachers(@RequestParam(required = false) String name) {
        return Result.success(stuHomeworkService.getTeachers(name));
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping
    public Result page(StuHomeworkQueryDTO stuHomeworkQueryDTO) {

        log.info("分页查询:{}",stuHomeworkQueryDTO);
        PageResult<StuHomeworkVO> pageResult= stuHomeworkService.page(stuHomeworkQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查看作业详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result detail(@PathVariable String id){
        log.info("查看作业详情id:{}",id);
        StuHomeworkVO stuHomeworkVO =stuHomeworkService.detail(id);//后续实现
        return Result.success(stuHomeworkVO);
    }


    /**
     * 新增作业
     * @param stuHomework
     * @return
     */


    @PostMapping()
    public Result save(@RequestBody StuHomework stuHomework){
        log.info("新增作业:{}",stuHomework);
//        stuHomework.setType("作业");
       stuHomeworkService.save(stuHomework);//后续实现
        return Result.success();
    }

    /**
     * 更新作业
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable String id, @RequestBody StuHomeworkUpdateDTO stuHomeworkUpdateDTO){
        log.info("更新作业的ID{}，数据{}",id,stuHomeworkUpdateDTO);
        stuHomeworkUpdateDTO.setId(id);
        stuHomeworkService.update(stuHomeworkUpdateDTO);//后续实现
        return Result.success();
    }

    /**
     * 删除单条作业
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        log.info("删除作业ID{}",id);
        stuHomeworkService.delete(id);//后续实现
        return Result.success();
    }

    /**
     * 批量删除作业
     * @param ids
     * @return
     */
    @PostMapping("/batch")
    public Result deleteBatch(@RequestBody List<String> ids) {  // 改为List<String>
        log.info("批量删除ID{}",ids);
        // 在service层转换为Integer
        stuHomeworkService.deleteBatch(ids.stream().map(Integer::parseInt).collect(Collectors.toList()));
        return Result.success();
    }




}
